#include <iostream>
#include <deque>

using namespace std;

void Progress(deque<int>& number, deque<char>& oper, char a);
int getOperPrec(char a, char b);    // a > b return 1, else return 0
char getIn(int& num);

int main()
{
    int times, num_temp, sum;
    char oper_temp, oper_top;
    deque<int> number;
    deque<char> oper;

    cin >> times;
    cin.get();
    for (int i = 0; i < times; i++) {
        oper_temp = getIn(num_temp);
        while (oper_temp != '\n' && oper_temp != '\r') {
            if (oper_temp == 0) {
                number.push_back(num_temp);
            }
            else if (oper.empty()) {
                oper.push_back(oper_temp);
            }
            else {
                oper_top = oper.back();
                if (getOperPrec(oper_temp, oper_top) > 0) {
                    oper.push_back(oper_temp);
                }
                else {
                    Progress(number, oper, oper_temp);
                }
            }
            oper_temp = getIn(num_temp);
            if (cin.fail())
                break;
        }
        int num1 = 0, num2 = number.back();
        number.pop_back();
        while (!oper.empty())
        {
            oper_temp = oper.back();
            oper.pop_back();
            num1 = num2;
            num2 = number.back();
            number.pop_back();
            switch (oper_temp)
            {
            case '^':
                if (num1 == 0) {
                    num2 = 1;
                    break;
                }
                sum = num2;
                for (int i = 1; i < num1; i++) {
                    sum *= num2;
                }
                num2 = sum;
                break;
            case '*':
                num2 *= num1; break;
            case '/':
                num2 /= num1; break;
            case '+':
                num2 += num1; break;
            case '-':
                num2 -= num1; break;
            default:break;
            }
        }
        cout << num2 << endl;
        number.clear();
        oper.clear();
    }
}

char getIn(int& num) {
    char ch, temp;
    cin.get(ch);
    if (cin.eof())
        return 0;
    if (ch == ' ')
        cin >> ch;
    if (ch == '-') {
        temp = cin.peek();
        if (temp == ' ')
            return ch;
        else {
            cin.putback(ch);
            cin >> num;
            return 0;
        }
    }
    if (ch >= '0' && ch <= '9') {
        cin.putback(ch);
        cin >> num;
        return 0;
    }
    return ch;
}

int getOperPrec(char a, char b) {
    if (a == '(' || b == '(')
        return 1;
    if (a == ')' || b == ')')
        return 0;
    if (a == '^')
    {
        if (b == '^')
            return 0;
        else return 1;
    }
    else if (a == '*' || a == '/')
    {
        if (b == '^' || b == '*' || b == '/')
            return 0;
        else return 1;
    }
    return 0;
}

void Progress(deque<int>& number, deque<char>& oper, char a) {
    int num1 = 0, num2 = 0, sum = 1;
    char op = oper.back();
    oper.pop_back();

    num2 = number.back();
    number.pop_back();

    if (a == ')') {
        while (op != '(') {
            num1 = num2;
            num2 = number.back();
            number.pop_back();

            switch (op)
            {
            case '^':
                if (num1 == 0) {
                    num2 = 1;
                    break;
                }
                sum = num2;
                for (int i = 1; i < num1; i++) {
                    sum *= num2;
                }
                num2 = sum;
                break;
            case '*':
                num2 *= num1; break;
            case '/':
                num2 /= num1; break;
            case '+':
                num2 += num1; break;
            case '-':
                num2 -= num1; break;
            default:break;
            }
            op = oper.back();
            oper.pop_back();
        }
        number.push_back(num2);
    }
    else {
        num1 = num2;
        num2 = number.back();
        number.pop_back();
        switch (op)
        {
        case '^':
            if (num1 == 0) {
                num2 = 1;
                break;
            }
            sum = num2;
            for (int i = 1; i < num1; i++) {
                sum *= num2;
            }
            num2 = sum;
            break;
        case '*':
            num2 *= num1; break;
        case '/':
            num2 /= num1; break;
        case '+':
            num2 += num1; break;
        case '-':
            num2 -= num1; break;
        default:break;
        }
        if (oper.empty()) {
            number.push_back(num2);
            oper.push_back(a);
            return;
        }
        if (getOperPrec(a, oper.back()) == 0)
        {
            num1 = num2;
            num2 = number.back();
            number.pop_back();
            op = oper.back();
            oper.pop_back();
            switch (op)
            {
            case '^':
                if (num1 == 0) {
                    num2 = 1;
                    break;
                }
                sum = num2;
                for (int i = 1; i < num1; i++) {
                    sum *= num2;
                }
                num2 = sum;
                break;
            case '*':
                num2 *= num1; break;
            case '/':
                num2 /= num1; break;
            case '+':
                num2 += num1; break;
            case '-':
                num2 -= num1; break;
            default:break;
            }
        }
        number.push_back(num2);
        oper.push_back(a);
    }
}